<?php

    class Review {

        private $ReviewId;
        private $Reviewer;
        private $Content;
        private $Product;

        public function __construct($reviewid, $reviewer, $content, $product) {
            $this->ReviewId = $reviewid;
            $this->Reviewer = $reviewer;
            $this->Content = $content;
            $this->Product = $product;
        }

        public function __get($key) {
            if(property_exists($this, $key)) {
                return $this->{$key};
            }
            else {
                return NULL;
            }
        }

        public function __set($key, $value) {
            throw new Exception('Class is immutable.');
        }

        public static function InsertReview($review) {
            Connection::Connect();
            if($try = Review::ReadReview($review->ReviewId) != NULL)
                return $try->ReviewId;

            $sql = mysql_query(
                "
                    INSERT INTO 
                    Review (
                        ReviewId, 
                        Reviewer, 
                        Content, 
                        Product
                    ) 
                    VALUES (
                        '$review->ReviewId', 
                        '$review->Reviewer->Username', 
                        '$review->Content', 
                        '$review->Product->ProductId'
                    )
                "
            );
            
            if($sql == FALSE)
                return NULL;

            $sql = mysql_query(
                "SELECT LAST_INSERT_ID() as 'LAST_INSERT_ID'"
            );
            
            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    return $row['LAST_INSERT_ID'];
                }
            }

            return NULL;
        }

        public static function ReadReview($reviewid) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    SELECT 
                        *
                    FROM 
                        Review 
                    WHERE
                        ReviewId = '$productid'
                "
            );
            
            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    $reviewid = $row['ReviewId'];
                    $reviewer = $row['Reviewer'];
                    $content = $row['Content'];
                    $product = $row['Product'];
                    return new Review($reviewid, Customer::ReadCustomer($reviewer), $content, Product::ReadProduct($product));
                }
            }
            return NULL;
        }

        public static function UpdateReview($review) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    UPDATE 
                        Review 
                    SET 
                        Reviewer = '$review->Reviewer->Username', 
                        Content = '$review->Content', 
                        Product = '$review->Product->ProductId' 
                    WHERE 
                        ReviewId = '$review->ReviewId'
                " 
            );
            
            if($sql == FALSE)
                return NULL;
            
            return $review->ReviewId;
        }

        public static function SearchReview($review) {
            Connection::Connect();
            $where = "WHERE ";
            $conds_present = FALSE;
            if($review->ReviewId != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "ReviewId = '$review->ReviewId' ";
            }
            if($review->Reviewer != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "Reviewer LIKE '%$review->Reviewer->Username%' ";
            }
            if($review->Content != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "Content LIKE '%$review->Content%' ";
            }
            if($review->Product != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "Product = '$review->Product->ProductId' ";
            }
            if(!$conds_present)
                $where = "";

            $sql = mysql_query(
                "
                    SELECT DISTINCT
                        *
                    FROM 
                        Review 
                "
                .
                $where
            );
            
            if($sql == NULL)
                return NULL;

            $items = array();
            while($row = mysql_fetch_assoc($sql))
            {
                $reviewid = $row['ReviewId'];
                $reviewer = $row['Reviewer'];
                $content = $row['Content'];
                $product = $row['Product'];

                $items[] = new Review($reviewid, Customer::ReadCustomer($reviewer), $content, Product::ReadProduct($product));
            }
            return items;
        }

    }

?>